package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Map getBusinessReportData(){
        //创建返回数据键值对
        HashMap<Object, Object> resultMap = new HashMap<>();

        //设计日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //日期处理
        Calendar today = Calendar.getInstance();
        //将时、分、秒、毫秒清零
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        //设置周一为每周第一天
        today.setFirstDayOfWeek(Calendar.MONDAY);
        //本周第一天
        Calendar weekFirstDay = (Calendar) today.clone();
        weekFirstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        //本周最后一天
        Calendar weekLastDay = (Calendar)today.clone();
        weekLastDay.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
        //本月第一天
        Calendar monthFirstDay = (Calendar) today.clone();
        monthFirstDay.set(Calendar.DAY_OF_MONTH, 1);
        //本月最后一天
        Calendar monthLastDay = (Calendar)today.clone();
        int maxDayOfMonth = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthLastDay.set(Calendar.DAY_OF_MONTH,maxDayOfMonth);

        //会员数据存储
        resultMap.put("reportDate",sdf.format(today.getTime()));
        resultMap.put("todayNewMember",memberDao.countByRegTimeRange(today.getTime(),today.getTime()));

        resultMap.put("totalMember",memberDao.getTotalMember());
        resultMap.put("thisWeekNewMember",memberDao.countByRegTimeRange(weekFirstDay.getTime(),weekLastDay.getTime()));
        resultMap.put("thisMonthNewMember",memberDao.countByRegTimeRange(monthFirstDay.getTime(),monthLastDay.getTime()));

        //预约数据
        resultMap.put("todayOrderNumber",orderDao.countByOrderDateRange(today.getTime(),today.getTime(),null));
        resultMap.put("thisWeekOrderNumber",orderDao.countByOrderDateRange(weekFirstDay.getTime(),weekLastDay.getTime(),null));
        resultMap.put("thisMonthOrderNumber",orderDao.countByOrderDateRange(monthFirstDay.getTime(),monthLastDay.getTime(),null));

        resultMap.put("todayVisitsNumber",orderDao.countByOrderDateRange(today.getTime(),today.getTime(), Order.ORDERSTATUS_YES));
        resultMap.put("thisWeekVisitsNumber",orderDao.countByOrderDateRange(weekFirstDay.getTime(),weekLastDay.getTime(),Order.ORDERSTATUS_YES));
        resultMap.put("thisMonthVisitsNumber",orderDao.countByOrderDateRange(monthFirstDay.getTime(),monthLastDay.getTime(),Order.ORDERSTATUS_YES));
        //热门套餐
        resultMap.put("hotSetmeal",orderDao.countByHotSetmeal(4));

        return resultMap;
    }

}
